package com.itcast.googleplay.holder;

import com.itcast.googleplay.R;
import com.itcast.googleplay.bean.AppInfo;
import com.itcast.googleplay.utils.UIUtils;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;

import android.R.array;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class AppDesHolder extends BaseHolder<AppInfo> implements OnGlobalLayoutListener{

	private TextView tvDes;
	private TextView tvAuthor;
	private ImageView ivArrow;
	private int minHeight;
	private int maxHeight;
	private LinearLayout llRoot;
	private boolean isOpen = false;
	private ValueAnimator valueAnimator;
	private LayoutParams layoutParams;
	private boolean isAnimation;
	private ScrollView scrollView;

	@Override
	public void initData() {
		AppInfo data = getData();
		
		tvDes.setText(Html.fromHtml(data.des));
		tvAuthor.setText(data.author);
		
		tvDes.setMaxLines(7);
		tvDes.getViewTreeObserver().addOnGlobalLayoutListener(this);
	}

	@Override
	public View initView() {
		View view = UIUtils.inflate(R.layout.layout_app_des);
		tvDes = (TextView) view.findViewById(R.id.tv_des);
		tvAuthor = (TextView) view.findViewById(R.id.tv_author);
		ivArrow = (ImageView) view.findViewById(R.id.iv_arrow);
		llRoot = (LinearLayout) view.findViewById(R.id.ll_root);
		
		llRoot.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				expend();
			}
		});
		
		return view;
	}

	protected void expend() {
		if(isAnimation){
			return;
		}
		if(!isOpen){
			//页面开启
			isOpen = true;
			if(maxHeight>minHeight){
				valueAnimator = ValueAnimator.ofInt(minHeight,maxHeight);
			}
		}else{
			isOpen = false;
			if(maxHeight>minHeight){
				valueAnimator = ValueAnimator.ofInt(maxHeight,minHeight);
			}
		}
		if(valueAnimator!=null){
			valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
				@Override
				public void onAnimationUpdate(ValueAnimator valueAnimator) {
					layoutParams.height = (Integer) valueAnimator.getAnimatedValue();
					tvDes.setLayoutParams(layoutParams);
					//触底的时候向上滚动
					scrollView.fullScroll(ScrollView.FOCUS_DOWN);
				}
			});
			
			valueAnimator.addListener(new AnimatorListener() {
				@Override
				public void onAnimationStart(Animator arg0) {
					isAnimation = true;
				}
				@Override
				public void onAnimationRepeat(Animator arg0) {
				}
				@Override
				public void onAnimationEnd(Animator arg0) {
					if(isOpen){
						ivArrow.setImageResource(R.drawable.arrow_up);
					}else{
						ivArrow.setImageResource(R.drawable.arrow_down);
					}
					isAnimation = false;
				}
				@Override
				public void onAnimationCancel(Animator arg0) {
					isAnimation = false;
				}
			});
			valueAnimator.setDuration(350);
			valueAnimator.start();
		}
	}

	@Override
	public void onGlobalLayout() {
		tvDes.getViewTreeObserver().removeGlobalOnLayoutListener(this);
		//获取最低高度
		minHeight = tvDes.getHeight();
		
		tvDes.setMaxLines(Integer.MAX_VALUE);
		tvDes.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			@Override
			public void onGlobalLayout() {
				maxHeight = tvDes.getHeight();
				tvDes.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				
				layoutParams = tvDes.getLayoutParams();
				layoutParams.height = minHeight;
				tvDes.setLayoutParams(layoutParams);
			}
		});
	}

	public void setScrollView(ScrollView scrollView) {
		this.scrollView = scrollView;
	}
}
